# -*- coding: utf-8 -*-
"""
@File: battle_system.py
@Time: 2021/12/14 14:49
@Author: 鹄望潇湘
@desc:
global_state{
    border: [1000, 1000]
    total_time: 1000
    last_time: 0
    leaderboard: {'0': 9.0, '1': 9.0}
}
{
    '0':{
        feature_layer: list()，在该玩家视野里所有人的分身所在的区域
        rectangle: (73, 0, 373, 300)
        overlap:{
            food: list(x, y, r)
            thorns: list(x, y, r)
            spore: list(x, y, r)
            clone: list(x, y, r, player, team)
        }
        team_name: '0'
    }
    ...
}

"""
import random

from my_submission.task import Task, EatFoodTask, MergeTask, BreakTask


class TaskManager:
    def __init__(self, player_name):
        self.tasks = []
        self.player_name = player_name

    def add_task(self, task: Task):
        if self.exists_task(task):
            return
        task.set_player_name(self.player_name)
        self.tasks.append(task)
        self.__sort_task__()

    def __sort_task__(self):
        self.tasks.sort(key=lambda x:x.priority, reverse=True)

    def get_action(self, global_state, player_state):
        action_ = self.tasks[0].get_action(global_state, player_state)
        if self.tasks[0].is_finish(global_state, player_state):
            self.tasks.pop(0)
        return action_

    def exists_task(self, task_obj) -> bool:
        """
        判断任务列表中是否已经存在了该任务

        :param task_obj:
        :return: True or False
        """
        for item in self.tasks:
            if type(item) == type(task_obj):
                return True
        return False

    def state_perception(self, global_state: dict, player_state: dict) -> None:
        # 随机分裂任务
        states = player_state[self.player_name]
        last_time = int(global_state['last_time'])

        if len(states['overlap']['clone']) > 8 and last_time > 50:
            self.add_task(MergeTask())
        elif last_time % 8 == 0 and last_time < 60:
            self.add_task(BreakTask())
        elif last_time % 15 == 0 and 120 > last_time > 60:
            self.add_task(BreakTask())
        elif last_time % 30 == 0 and len(states['overlap']['clone']) < 8:
            self.add_task(BreakTask())




class PlayerDispatcher:
    """
    某个Player移动策略调度器
    """
    def __init__(self, player_name: str):
        self.tasks = TaskManager(player_name)
        self.player_name = player_name

        pass

    def get_action(self, global_state, player_state):
        return self.tasks.get_action(global_state, player_state)
        pass


class TeamDispatcher:
    """
    队伍移动策略调度器
    """
    def __init__(self, team_name, players):
        self.team_name = team_name
        self.players = players
        self.player_dispatcher = dict()
        for player in players:
            self.player_dispatcher[player] = TaskManager(player)
            self.player_dispatcher[player].add_task(EatFoodTask())  # 默认添加自动吃食物的任务

    def get_action(self, global_state: dict, player_state: dict) -> dict:
        return {player: self.player_dispatcher[player].get_action(global_state, player_state) for player in self.players}
        pass

    def state_perception(self, global_state: dict, player_state: dict) -> None:
        """
        通过当前环境能够或得到的信息(如位置，球的大小, 其他球的位置等)计算需要执行的task
        :param global_state:
        :param player_state:
        :return: None
        """
        for player in self.players:
            self.player_dispatcher[player].state_perception(global_state, player_state)
        pass


if __name__=='__main__':
    task_manager = TaskManager("a")
    task_manager.add_task(BreakTask())
    task_manager.add_task(EatFoodTask())
    task_manager.add_task(MergeTask())
    action = task_manager.get_action({"a": 3}, {"b":4})
    print(task_manager)